#include "caddoor.h"

static QStringList LIST_NAME = (QStringList() << "门");
static QStringList LIST_LIGHT_OPEN = (QStringList() << "开" << "打开" );
static QStringList LIST_LIGHT_CLOSE = (QStringList() << "关" << "关闭");

CADDoor::CADDoor(QObject *parent):CAudioControlDevice(parent)
{

}

void CADDoor::addDevice(DEVICE_OPERATION *d)
{
    _device_list.append(d);
}

void CADDoor::clearDevice()
{
    foreach (DEVICE_OPERATION *d, _device_list) {
        d->deleteLater();
    }

    _device_list.clear();
}

DEVICE_OPERATION * CADDoor:: parseWord(QString word)
{
    QString op,name,value;

    foreach (QString s, LIST_NAME) {
        if(word.contains(s))
        {
            name = s;
            break;
        }
    }

    foreach (QString s, LIST_LIGHT_OPEN) {
        if(word.contains(s))
        {
            op = DEVICE_ACTION_SWITCH;
            value = DEVICE_STATUS_ON;
            break;
        }
    }

    foreach (QString s, LIST_LIGHT_CLOSE) {
        if(word.contains(s))
        {
            op = DEVICE_ACTION_SWITCH;
            value = DEVICE_STATUS_OFF;
            break;
        }
    }

    if(name.isEmpty() || op.isEmpty() || value.isEmpty())
    {
        qDebug()<< "Audio: could not found device " << word;
        return nullptr;
    }

    DEVICE_OPERATION *device_op = new DEVICE_OPERATION();
    device_op->_audio_name = name;
    device_op->_audio_operation = op;
    device_op->_audio_value = value;

    return device_op;
}

bool CADDoor::actionAudionControl(DEVICE_OPERATION *devOp)
{
    devOp->_match = false;

    foreach (DEVICE_OPERATION *d, _device_list) {
        if(d->_name.contains(devOp->_audio_name) )
        {
            devOp->_id = d->_id;
            devOp->_name = d->_name;
            devOp->_match = true;

            return true;
        }
    }

    return false;
}
